package szcg.framework.library.base;

import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import szcg.framework.library.base.service.BaseHandleService;
import szcg.framework.library.base.service.RequestLockService;
import szcg.framework.library.base.service.SessionService;
import szcg.framework.library.base.service.TokenService;
import szcg.framework.library.config.constant.ServiceNameConfig;

import java.util.HashMap;

/**
 * 基类
 */
public abstract class PostController extends BaseEncryption {
    private static final Logger logger = LoggerFactory.getLogger(PostController.class);

    //子类具体实现
    protected abstract String postRequest(HashMap<String, String> requestParam);

    @Autowired
    public BaseHandleService baseHandleService; //处理基础请求

    @Autowired
    public RequestLockService requestLockService; //请求锁

    @Autowired
    public SessionService sessionService; //session

    @Autowired
    public TokenService tokenService; //token

    protected String handleRequest(HttpServletRequest req) {
        //获取原始请求信息
        HashMap<String, String> requestInfoMap = baseHandleService.getRequestInfo(req);
        String serveName = requestInfoMap.get("serveName");
        //校验
        HashMap<String, String> serveNameHashMap = baseHandleService.verifierServiceName(serveName);
        if (!serveNameHashMap.get("state").equals("000")) {
            return generateFail(serveNameHashMap.get("msg"));
        }
        //接口锁
        HashMap<String, String> requestLockMap = requestLockService.verifierLock(requestInfoMap);
        if (!requestLockMap.get("state").equals("000")) {
            //说明锁生效
            return generateFail(requestLockMap.get("msg"));
        }
        //判断api类型
        String type = ServiceNameConfig.registerMap.get(serveName);
        return switch (type) {
            case "-1" -> generateFail("该api已停止请求");
            case "0" -> notNeedTokenRequest(requestInfoMap);
            case "1" -> needTokenRequest(requestInfoMap);
            case "2" -> postRequest(requestInfoMap);
            default -> generateFail("错误的api请求");
        };
    }

    //处理不需要token的请求
    private String notNeedTokenRequest(HashMap<String, String> requestInfoMap) {
        String sessionId = requestInfoMap.get("sessionId");
        String requestBody = requestInfoMap.get("requestBody");
        HashMap<String, String> sessionMap = sessionService.verifier(sessionId);
        if (!sessionMap.get("state").equals("000")) {
            return generateFail(sessionMap.get("msg"));
        }
        String realBody = baseHandleService.decryptRequestBody(requestBody);
        requestInfoMap.put("requestBody", realBody);//解密body
        return postRequest(requestInfoMap);
    }

    //处理需要token的请求
    private String needTokenRequest(HashMap<String, String> requestInfoMap) {
        String sessionId = requestInfoMap.get("sessionId");
        String requestBody = requestInfoMap.get("requestBody");
        HashMap<String, String> sessionMap = sessionService.verifier(sessionId);
        if (!sessionMap.get("state").equals("000")) {
            return generateFail(sessionMap.get("msg"));
        }
        String realBody = baseHandleService.decryptRequestBody(requestBody);
        requestInfoMap.put("requestBody", realBody);//解密body
        //普通接口->校验token的合法性->token
        HashMap<String, String> tokenVerifier = tokenService.verifier(realBody);
        if (!tokenVerifier.get("state").equals("000")) {
            return generateFail(tokenVerifier.get("msg"));
        }
        return postRequest(requestInfoMap);
    }


    protected String generateSessionId() {
        return sessionService.generateSessionId();
    }

    protected String generateToken(String userId) {
        return tokenService.generateToken(userId);
    }
}
